package vn.personal.anhht.bloback12.activity;

import vn.personal.anhht.bloback12.block.BlockIncomingCall;
import vn.personal.anhht.bloback12.block.OnBootBlock;
import vn.personal.anhht.bloback12.util.Constants;
import android.content.ComponentName;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;

public class TabSettingGeneralActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
    SharedPreferences prefs = null;
    private CheckBoxPreference checkPass;
    private CheckBoxPreference checkTime;
    private Resources rs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName(
                Constants.SETTINGS_SHARED_PREFERENCES_NAME);
        rs = getResources();
        addPreferencesFromResource(R.xml.preferences_general);
        checkPass = (CheckBoxPreference) findPreference(rs
                .getString(R.string.key_pref_turn_pass));
        checkTime = (CheckBoxPreference) findPreference("alarm");
        
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }


    @Override
    protected void onStart() {
        Log.e("TabSettingCallActivity", "onStart");
        super.onStart();
        if (checkPass.isChecked()) {
            checkPass.setSummary(rs
                    .getString(R.string.key_pref_turn_pass_summary_1));
        }
        if (!checkPass.isChecked()) {
            checkPass.setSummary(rs
                    .getString(R.string.key_pref_turn_pass_summary));
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        Log.e("Key", key);
        if ((rs.getString(R.string.key_pref_turn_pass)).equals(key)) {
            Log.e("Tab Setting General", "Turn pass");
            if (checkPass.isChecked()) {
                checkPass.setSummary(rs
                        .getString(R.string.key_pref_turn_pass_summary_1));
            } else {
                checkPass.setSummary(rs
                        .getString(R.string.key_pref_turn_pass_summary));
            }
        }
        if ("alarm".equals(key)) {
            ComponentName component1 = new ComponentName(
                    TabSettingGeneralActivity.this, BlockIncomingCall.class);
            // and turn off receiver
            getPackageManager().setComponentEnabledSetting(component1,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
            if (checkTime.isChecked()) {
                OnBootBlock.setAlarm(TabSettingGeneralActivity.this);
            } else {
                getPackageManager().setComponentEnabledSetting(component1,
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
                OnBootBlock.cancelAlarm(TabSettingGeneralActivity.this);
            }
        } else if ("alarm_time1".equals(key)) {
            OnBootBlock.cancelAlarm(TabSettingGeneralActivity.this);
            OnBootBlock.setAlarm(TabSettingGeneralActivity.this);
        } else if ("alarm_time2".equals(key)) {
            OnBootBlock.cancelAlarm(TabSettingGeneralActivity.this);
            OnBootBlock.setAlarm(TabSettingGeneralActivity.this);
        }
        
    }
}